// Copyright 2019 Google, LLC.  All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
esid: prod-OptionalExpression
description: >
  optional chain returning undefined in RHS of for of statement
info: |
  IterationStatement
    for (LeftHandSideExpression of Expression) Statement
features: [optional-chaining]
---*/

function Test() {
    assert.throws(TypeError, function() {
      for (const key of {}?.a) ;
    });

    assert.throws(TypeError, function() {
      for (const key of {}?.a) {}
    });

    const obj = undefined;
    assert.throws(TypeError, function() {
      for (const key of obj?.a) {}
    });

    assert.throws(TypeError, function() {
      for (const key of obj?.a);
    });
}

// preheat
const NUMBER_OF_PREHEAT = 1;
for (let i = 0; i < NUMBER_OF_PREHEAT; i++) {
    Test();
}

ArkTools.jitCompileAsync(Test);
assert.sameValue(ArkTools.waitJitCompileFinish(Test), true, '[JIT] JIT disabled.');

// test
const NUMBER_OF_TEST = 1; // or 2
for (let i = 0; i < NUMBER_OF_PREHEAT; i++) {
    Test();
}
